/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.view;

import com.controller.BeberageManager;
import com.domain.Beberage;
import com.domain.BeberageTableModel;
import com.domain.BeberagesList;
import com.gui.BeberageManagementForm;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

/**
 *
 * @author Miguel
 */
public class BeberageManagementView implements ActionListener {

    public BeberageManagementView() {
        initializeBeberageManager();
        initializeManagementForm();
        initializeButtons();
        initializeBeberageListTable();
        loadBeberageListTable();
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        String buttonClicked = actionEvent.getActionCommand();
        switch (buttonClicked) {
            case ADD_BEBERAGE:
                showCreateBeberageForm();
                break;
            case DELETE_BEBERAGE:
                deleteBeberage();
                break;
            case SEARCH_BEBERAGE:
                break;
            case MODIFY_BEBERAGE:
                showViewBeberageForm();
                break;
            case VIEW_BEBERAGE:
                showViewBeberageForm();
                break;
        }
        loadBeberageListTable();
    }

    public void showBeberageManagementForm() {
        managementForm.setVisible(true);
    }

    public void showMessageToForm(String msg) {
        JOptionPane.showMessageDialog(managementForm, msg, "Mensaje", JOptionPane.INFORMATION_MESSAGE);
    }

    private void showViewBeberageForm() {
        if (isABeberageSelected()) {
            Beberage viewingBeberage = getSelectedBeberageFromList();
            ShowBeberageInformationView beberageInfoView = new ShowBeberageInformationView(viewingBeberage);
            beberageInfoView.showBeberageInformationForm();
        } else {
            showMessageToForm("Favor de Seleccionar una Bebida, para Mostrar");
        }
    }

    private void showModifyBeberageForm() {
        if (isABeberageSelected()) {
        } else {
            showMessageToForm("Favor de Seleccionar una Bebida, para Modificar");
        }
    }

    private void deleteBeberage() {
        if (isABeberageSelected()) {
            Beberage deletingBeberage = getSelectedBeberageFromList();
            boolean isBeberageDeletionConfirmed = confirmDeleteBeberage();
            if (isBeberageDeletionConfirmed) {
                removeBeberageFromInventory(deletingBeberage);
            }
        } else {
            showMessageToForm("Favor de Seleccionar una Bebida a Eliminar");
        }
    }

    private void removeBeberageFromInventory(Beberage deletingBeberage) {
        beberageManager.removeBeberage(deletingBeberage,this);
    }

    private boolean isABeberageSelected() {
        int selectedBeberageIndex = beberageTable.getSelectedRow();
        return selectedBeberageIndex >= 0;
    }

    private Beberage getSelectedBeberageFromList() {
        Beberage selectedBeberage = null;
        int selectedBeberageIndex = beberageTable.getSelectedRow();
        selectedBeberage = getBeberageFromTableList(selectedBeberageIndex);
        return selectedBeberage;
    }

    private Beberage getBeberageFromTableList(int beberageTableIndex) {
        BeberageTableModel beberageTableList = (BeberageTableModel) beberageTable.getModel();
        Beberage selectedBeberage = beberageTableList.getBeberageAt(beberageTableIndex);
        return selectedBeberage;
    }

    private boolean confirmDeleteBeberage() {
        int confirmDeleteBeberage = JOptionPane.showConfirmDialog(
                managementForm,
                "¿Desea Eliminar la Bebida Seleccionada?",
                "Eliminar Bebida",
                JOptionPane.YES_NO_OPTION);
        return confirmDeleteBeberage == JOptionPane.YES_OPTION;
    }

    private void initializeBeberageListTable() {
        BeberageTableModel beberageTableList = new BeberageTableModel();
        beberageTableList.addColumn("Beberage ID");
        beberageTableList.addColumn("Beberage Name");
        beberageTableList.addColumn("Beberage Description");
        beberageTable = managementForm.getBeberageList();
        beberageTable.setModel(beberageTableList);
        beberageTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }

    private void showCreateBeberageForm() {
        CreateBeberageView createBeberageView = new CreateBeberageView();
        createBeberageView.showBeberageCreationForm();
    }

    private void initializeBeberageManager() {
        beberageManager = BeberageManager.getInstance();
    }

    private void initializeManagementForm() {
        managementForm = new BeberageManagementForm();
        managementForm.setTitle("Administración de Bebidas");
    }

    private void loadBeberageListTable() {
        BeberagesList beberagesList = beberageManager.getBeberageList();
        populateBeberageTable(beberagesList);
    }

    private void populateBeberageTable(BeberagesList beberagesList) {
        BeberageTableModel beberageTableModel = (BeberageTableModel) beberageTable.getModel();
        resetBeberageListTable(beberageTableModel);

        for (int beberageListIndex = 0; beberageListIndex < beberagesList.getTotalBeberages(); beberageListIndex++) {
            Beberage actualBeberage = beberagesList.getBeberage(beberageListIndex);
            beberageTableModel.addBeberageToTable(actualBeberage);
        }

        beberageTable.setModel(beberageTableModel);
    }

    private void resetBeberageListTable(BeberageTableModel beberageListTable) {
        beberageListTable.setRowCount(0);
    }

    private void initializeButtons() {
        addBeberageButton = managementForm.getAddBeberageButton();
        addBeberageButton.setActionCommand(ADD_BEBERAGE);
        addBeberageButton.addActionListener(this);

        deleteBeberageButton = managementForm.getDeleteBeberageButton();
        deleteBeberageButton.setActionCommand(DELETE_BEBERAGE);
        deleteBeberageButton.addActionListener(this);

        modifyBeberageButton = managementForm.getModifyBeberageButton();
        modifyBeberageButton.setActionCommand(MODIFY_BEBERAGE);
        modifyBeberageButton.addActionListener(this);

        searchBeberageButton = managementForm.getSearchBeberageButton();
        searchBeberageButton.setActionCommand(SEARCH_BEBERAGE);
        searchBeberageButton.addActionListener(this);

        viewBeberageButton = managementForm.getViewBeberageButton();
        viewBeberageButton.setActionCommand(VIEW_BEBERAGE);
        viewBeberageButton.addActionListener(this);
    }
    private final String ADD_BEBERAGE = "ADD_BEBERAGE";
    private final String DELETE_BEBERAGE = "DELETE_BEBERAGE";
    private final String MODIFY_BEBERAGE = "MODIFY_BEBERAGE";
    private final String VIEW_BEBERAGE = "VIEW_BEBERAGE";
    private final String SEARCH_BEBERAGE = "SEARCH_BEBERAGE";
    private BeberageManagementForm managementForm;
    private JButton addBeberageButton;
    private JButton deleteBeberageButton;
    private JButton modifyBeberageButton;
    private JButton searchBeberageButton;
    private JButton viewBeberageButton;
    private JTable beberageTable;
    private BeberageManager beberageManager;
}
